<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
        #menu {
            width: 200px;
            height: 200px;
            background: red;
            display: none;
        }
    </style>
</head>

<body>
    <button id="btn">点击弹出</button>
    <div id="menu"></div>
    <script>
        let btn = document.getElementById("btn");
        let menu = document.getElementById("menu");
        document.onclick = function(e) {
            console.log(e.target.id);
            if (e.target.id == "btn") {
                menu.style.display = "block";
            }

            if (e.target.id != "btn" && e.target.id != "menu") {
                menu.style.display = "none";
            }
        };
        //事件委托
    </script>
</body>

</html>